package de.tabuma.lib;

import java.util.*;
import java.lang.reflect.*;
import java.util.logging.*;

/**
 *
 * @author alefen
 */
public class view {

    private static final Logger log = Logger.getLogger(view.class.getName());
    public static final int TYPE_FIELD = 1;
    public static final int TYPE_TEXTAREA = 2;
    public static final int TYPE_PASSWORD = 3;
    public static final int TYPE_LIST = 4;
    public static final int TYPE_CHECKBOX = 5;
    public static final int TYPE_LIST_STRING = 6;

    public static final String TRUE_VALUE = "true";
    public static final String FALSE_VALUE = "false";

    public static String getLinkAsAttribute(Hashtable parameter) {
        String link = "";
        Enumeration enu = parameter.keys();
        boolean firstElement = false;
        while(enu.hasMoreElements()) {
            String key = (String) enu.nextElement();
            String value = "" + parameter.get(key);
            if(firstElement) {
                link += "&amp;";
            }
            link += key + "=" + value;
            firstElement = true;
        }
        return link;
    }

    public static String getHiddenAsAttribute(Hashtable parameter) {
        String link = "";
        Enumeration enu = parameter.keys();
        while(enu.hasMoreElements()) {
            String key = (String) enu.nextElement();
            String value = "" + parameter.get(key);
            link += "<input type=\"hidden\" name=\""+key+"\" value=\""+value+"\"/>\n";
        }
        return link;
    }

    public static String getFormBeginn(String servlet, String linkName) {
        return "<form action=\""+servlet + (linkName!=null ? "#" + linkName : "") +"\">";
    }

    public static String getFormEnd() {
        return "</form>";
    }

    public static String printHiddenField(String key, String value) {
       return "<input type=\"hidden\" name=\""+key+"\" value=\""+value+"\"/>";
    }

    public static String printListField(String key, String value, List contentList, List<Field> displayedFields, String cssClass) {
        String str = "";
        try {
            str += "<select name=\""+key+"\" "+(cssClass != null ? "class=\""+cssClass+"\"" : "")+">";
            if(value==null) {
                value = "0";
            }
            if(value.equals("")) {
                value = "0";
            }
            int valueInt = Integer.valueOf(value).intValue();
            for(int i=0;i<contentList.size();i++) {
                Object obj = contentList.get(i);
                String id = ""+obj.getClass().getMethod("getId").invoke(obj);
                int intId = Integer.valueOf(id).intValue();
                if(intId==valueInt) {
                    str += "<option value=\""+id+"\" selected>";
                }
                else {
                    str += "<option value=\""+id+"\">";
                }
                for(int j=0;j<displayedFields.size();j++) {
                    Field displayedField = displayedFields.get(j);
                    String getter = ObjectMethods.getGetter(displayedField.getName());
                    if(j!=0) {
                        str += ", ";
                    }
                    str += obj.getClass().getMethod(getter).invoke(obj);
                }
                str += "</option>";
            }
            str += "</select>";
        }
        catch(Exception e) {
            String warning = e.getMessage() + "/" + e.getCause() + "/" + e.getClass()+"\n";
            for(int i=0;i<e.getStackTrace().length;i++) {
                warning += e.getStackTrace()[i] + "\n";
            }
            log.warning(warning);
        }
        return str;
    }

    public static String printListStringField(String key, String value, List contentList, List<Field> displayedFields, String cssClass) {
        String str = "";
        try {
            str += "<select name=\""+key+"\" "+(cssClass !=null ? "class=\""+cssClass+"\"" : "")+">";
            if(value==null) {
                value = "";
            }
            for(int i=0;i<contentList.size();i++) {
                Object obj = contentList.get(i);
                String id = ""+obj.getClass().getMethod("getId").invoke(obj);
                if(id.equals(value)) {
                    str += "<option value=\""+id+"\" selected>";
                }
                else {
                    str += "<option value=\""+id+"\">";
                }
                for(int j=0;j<displayedFields.size();j++) {
                    Field displayedField = displayedFields.get(j);
                    String getter = ObjectMethods.getGetter(displayedField.getName());
                    if(j!=0) {
                        str += ", ";
                    }
                    str += obj.getClass().getMethod(getter).invoke(obj);
                }
                str += "</option>";
            }
            str += "</select>";
        }
        catch(Exception e) {
            String warning = e.getMessage() + "/" + e.getCause() + "/" + e.getClass()+"\n";
            for(int i=0;i<e.getStackTrace().length;i++) {
                warning += e.getStackTrace()[i] + "\n";
            }
            log.warning(warning);
        }
        return str;
    }

    public static String printCheckboxField(String key, String value) {

        String str = "";
        boolean isTrue = false;
        if(value!=null) {
            if(value.equals("true")) {
                isTrue = true;
            }
        }
        if(isTrue) {
            str += "<input type=\"checkbox\" name=\""+key+"\" value=\""+TRUE_VALUE+"\" checked=\"checked\"/>";
        }
        else {
            str += "<input type=\"checkbox\" name=\""+key+"\" value=\""+TRUE_VALUE+"\"/>";
        }
        return str;
    }

    public static String printInputField(String key, String value, String cssClass) {
        return "<input type=\"text\" name=\""+key+"\" value=\""+(value==null ? "" : value)+"\" "+(cssClass!=null ? "class=\""+cssClass+"\"" : "")+"/>";
    }

    public static String printPasswordField(String key, String value, String cssClass) {
        return "<input type=\"password\" name=\""+key+"\" value=\""+(value==null ? "" : value)+"\" "+(cssClass!=null ? "class=\""+cssClass+"\"" : "")+"/>";
    }

    public static String printTextareaField(String key, String value, String cssClass) {
        String str = "";
        str += "<textarea name=\""+key+"\" "+(cssClass!=null ? "class=\""+cssClass+"\"":"")+">" + value + "</textarea>";
        return str;
    }

    private static String printForm(String key, String keyField, String value, int nr, int kind, List list, List<Field> displayedFields, String labelClass, String fieldClass) {
        String form = "";
        form += "<div class=\"col"+nr+"\">\n";
        form += "<div class=\"col"+nr+"_head"+(labelClass!=null ? labelClass : "")+"\">"+key+"</div>\n";
        switch(kind) {
            case TYPE_LIST:
                form += "<div class=\"col"+nr+"_content\">"+printListField(keyField, value, list, displayedFields, fieldClass)+"</div>\n";
                break;
            case TYPE_LIST_STRING:
                form += "<div class=\"col"+nr+"_content\">"+printListStringField(keyField, value, list, displayedFields, fieldClass)+"</div>\n";
                break;
            case TYPE_TEXTAREA:
                form += "<div class=\"col"+nr+"_content\">"+printTextareaField(keyField, value, fieldClass)+"</div>\n";
                break;
            case TYPE_PASSWORD:
                form += "<div class=\"col"+nr+"_content\">"+printPasswordField(keyField, value, fieldClass)+"</div>\n";
                break;
            case TYPE_CHECKBOX:
                form += "<div class=\"col"+nr+"_content\">"+printCheckboxField(keyField, value)+"</div>\n";
                break;
            default:
                form += "<div class=\"col"+nr+"_content\">"+printInputField(keyField, value, fieldClass)+"</div>\n";
                break;
        }
        form += "</div>\n";
        return form;
    }

    public static String printForm1(String key, String keyField, String value) {
        return printForm(key, keyField, value, 1, TYPE_FIELD, null, null, null,null);
    }

    public static String printForm1(String key, String keyField, String value, int kind, List listParameterContent, List<Field> listParameterDisplayedField, String labelClass, String cssClass) {
        return printForm(key, keyField, value, 1, kind, listParameterContent, listParameterDisplayedField, labelClass, cssClass);
    }

    public static String printForm2(String key, String keyField, String value) {
        return printForm(key, keyField, value, 2, TYPE_FIELD, null, null, null, null);
    }

    public static String printForm2(String key, String keyField, String value, int kind, List list, List<Field> displayFields, String labelClass, String cssClass) {
        return printForm(key, keyField, value, 2, kind, list, displayFields, labelClass, cssClass);
    }

    public static String getContentHeader(String contentHeader) {
        String str = "";
        str += "<div class=\"contentHeader\">"+contentHeader+"</div>";
        return str;
    }

    public static String getContentTableBeginn() {
        return "<table class=\"contenttable\">\n";
    }

    public static String getContentTableEnd() {
        return "</table>\n";
    }

    public static String getContentHeader(Vector<String> tabHeadElements) {
        if(tabHeadElements==null) {
            return null;
        }
        String str = "<tr>\n";
        for(int i=0;i<tabHeadElements.size();i++) {
            str += "<th class=\"contentth\">" + tabHeadElements.elementAt(i)+"</th>\n";
        }
        str += "</tr>\n";
        return str;
    }

    public static String getTableContent(Vector<String> content) {
        if(content==null) {
            return "";
        }
        String str = "";
        str += "<tr>\n";
        for(int i=0;i<content.size();i++) {
            str += "<td class=\"contenttd\">"+content.elementAt(i)+"</td>\n";
        }
        str += "</tr>\n";
        return str;
    }

    public static String getTable(String servlet,Hashtable parameter, Vector<String> headElements, Vector<Vector<String>> elements, Hashtable addParameter, String addKey) {
        String noEntry = "no Entry";
        String add = "add";
        String str = "";
        str += "<div class=\"contentContent\">\n";
        str += getContentTableBeginn();
        str += getContentHeader(headElements);
        for(int i=0;i<elements.size();i++) {
            str += getTableContent(elements.elementAt(i));
        }
        if(elements.size()==0) {
            str += "<tr>\n";
            str += "<td class=\"contenttd\" colspan=\""+headElements.size()+"\">"+noEntry+"</td>\n";
            str += "</tr>\n";
        }
        str += getContentTableEnd();
        if(addParameter!=null) {
            str += getFormBeginn(servlet, null);
            str += getHiddenAsAttribute(parameter);
            str += getHiddenAsAttribute(addParameter);
            str += "<input type=\"submit\" name=\""+addKey+"\" value=\""+add+"\" class=\"button\"/>\n";
            str += getFormEnd();
        }
        str += "</div>\n";
        return str;
    }
}